package ice;

import ice.utils.Logger;
import ice.utils.SortedProperties;

import java.util.Properties;

import org.apache.commons.lang3.math.NumberUtils;

/**
 * Environment
 * 
 * @author abiao
 */
public class Environment {

    private static final Logger logger = Logger.getLogger(Environment.class);
    private static Environment ENVIRONMENT_INSTANCE = null;
    private SortedProperties props = null;

    public static final String MEM_DB_URL = "mem.db.url";
    public static final String FILE_DB_URL = "file.db.url";
    public static final String MEM_DB_DRIVER = "mem.db.driver";
    public static final String FILE_DB_DRIVER = "file.db.driver";
    public static final String MEM_DB_USERNAME = "mem.db.username";
    public static final String MEM_DB_PASSWORD = "mem.db.password";
    public static final String FILE_DB_USERNAME = "file.db.username";
    public static final String FILE_DB_PASSWORD = "file.db.password";
    public static final String DBCP_WHEN_EXHAUSTED_ACTION = "dbcp.when.exhausted.action";

    public Environment() {
    }

    public static String get(String name) {
        return getInstance().getProperty(name);
    }

    public static Properties getInstance() {
        if (ENVIRONMENT_INSTANCE == null) {
            ENVIRONMENT_INSTANCE = new Environment();
        }
        return ENVIRONMENT_INSTANCE.props;
    }

    public static byte getByte(String name) {
        return (byte) getInt(name);
    }

    private static int getInt(String name) {
        int value = NumberUtils.toInt(get(name), -1);
        if (value == -1) {
            logger.error(name + "不能转换为整型数值");
        }
        return value;
    }
}
